package com.hmdp;


import com.hmdp.constants.RedisConstant;
import com.hmdp.entity.Shop;
import com.hmdp.service.impl.ShopServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.geo.Point;
import org.springframework.data.redis.core.StringRedisTemplate;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@SpringBootTest
@Slf4j
public class testSaveShopToRedis
{

    @Autowired
    private ShopServiceImpl shopService;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;


    @Test
    public void testSaveShopRedis()
    {
        shopService.saveShopToRedis(2L, 30L);
    }

    @Test
    public void testSaveShopRedisALL()
    {
//        先查询店铺分组
        List<Shop> list = shopService.list();
//        然后给店铺分组，按照typeid分组，每一个组放进一个集合
        Map<Long, List<Shop>> collect = list.stream().collect(Collectors.groupingBy(Shop::getTypeId));
//        分批完成写入redis
        for(Map.Entry<Long, List<Shop>> entry : collect.entrySet()){
//            获取类型id，以类型id为key，店铺id列表为value
            Long typeId = entry.getKey();
            String key = RedisConstant.SHOP_GEO_KEY  + typeId;
            List<Shop> value = entry.getValue();

//            List<RedisGeoCommands.GeoLocation<String>> locations = new ArrayList<>();

            for(Shop shop: value){
                stringRedisTemplate.opsForGeo().add(key,new Point(shop.getX(),shop.getY()),shop.getId().toString());
            }

        }
    }


    @Test
    public void testHyperLogLog(){

        String[] values = new String[1000];
//        初始化
        int j = 0;
        for(int i = 0; i < 1000000; i++){
            j = i % 1000;
            values[j] = String.valueOf(i);
            if(j == 999){
                stringRedisTemplate.opsForHyperLogLog().add("hl1", values);
                j = 0;
            }
        }

//        统计结果
        Long count = stringRedisTemplate.opsForHyperLogLog().size("hl1");
        log.info("统计结果：{}",count);
//        stringRedisTemplate.opsForHyperLogLog().add("hl1","1","2","3");
    }

}
